package com.company.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author lcl
 * @Title: HttpClientUtils
 * @ProjectName smart-planet
 * @Description: http 请求util
 * @date 2019/1/17上午11:48
 */
public class HttpClientUtils {

    public Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     *  根据url get 请求
     * @param url
     * @return
     */
    public static JSONObject doGetJson(String url){
        JSONObject jsonbject = null;
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response;
        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                String result = EntityUtils.toString(entity,"UTF-8");
                jsonbject = JSONObject.parseObject(result);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        httpGet.releaseConnection();
        return jsonbject;
    }

    /**
     * 根据url post 请求
     * @param url
     * @param jsonObject  参数
     * @return
     * @throws IOException
     */
    public static JSONObject doPostJson(String url,JSONObject jsonObject){
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(jsonObject.toString(),"utf-8");
            s.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 返回json格式：
                HttpEntity httpEntity = res.getEntity();
                String result = EntityUtils.toString(httpEntity,"utf-8");
                response = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

    /**
     * 根据url post 请求
     * @param url
     * @param param  参数
     * @return
     * @throws IOException
     */
    public static String doPostString(String url,String param){
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        String response = null;
        try {
            StringEntity s = new StringEntity(param,"utf-8");
            s.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 返回json格式：
                HttpEntity httpEntity = res.getEntity();

                response = EntityUtils.toString(httpEntity,"utf-8");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

    /**
     * 根据url post 请求
     * @param url
     * @param param  参数
     * @return
     * @throws IOException
     */
    public static byte[] doPostBytes(String url,String param){
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        byte[] response = null;
        try {
            StringEntity s = new StringEntity(param,"utf-8");
            s.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 返回json格式：
                HttpEntity httpEntity = res.getEntity();
                response = EntityUtils.toByteArray(httpEntity);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
}
